//
//  QRCMyCardViewController.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/30.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit

class QRCMyCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        iconView.image = getQRCodeImage()
        
    }
    
    // 获取二维码
    func getQRCodeImage() -> UIImage {
        // 1.建立二维码滤镜（神马？）
        let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
        // 2.设置成初始值（神马？）
        qrFilter.setDefaults()
        // 3.通过KVC设置滤镜信息(神神马？？)
        qrFilter.setValue("哎呀哎呀".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        // 4.获取结果
        let ciImage = qrFilter.outputImage
        // 生成图片的大小
        print(ciImage.extent)
        // 6.放大图片，否则图片不清晰
        let transform = CGAffineTransformMakeScale(15, 15)
        let transformImage = ciImage.imageByApplyingTransform(transform)
        
        // 7.颜色滤镜（神神神马？？）
        let colorFilter = CIFilter(name: "CIFalseColor")!
        colorFilter.setDefaults()
        colorFilter.setValue(transformImage, forKey: "inputImage")
        // 前景色
        colorFilter.setValue(CIColor(color: UIColor.blackColor()), forKey: "inputColor0")
        // 背景色
        colorFilter.setValue(CIColor(color: UIColor.whiteColor()), forKey: "inputColor1")
        
        let resultImage = colorFilter.outputImage
        let avatar = UIImage(named: "avatar")!
        return insertAvatarImage(UIImage(CIImage: resultImage), avatar: avatar)
        
    }
    
    // 在二维码图像中间增加一个头像
    private func insertAvatarImage(image: UIImage, avatar: UIImage) -> UIImage {
        
        // 1. 开启绘图上下文，和二维码图像相同大小
        UIGraphicsBeginImageContext(image.size)
        
        // 2. 绘图
        let rect = CGRect(origin: CGPointZero, size: image.size)
        image.drawInRect(rect)
        
        // 添加头像
        let w = rect.width * 0.25  // 宽度是整个的四分之一
        let x = (rect.width - w) * 0.5
        let y = (rect.height - w) * 0.5
        avatar.drawInRect(CGRect(x: x, y: y, width: w, height: w))
        
        // 3. `先`取得结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        // 4. `后`关闭上下文
        UIGraphicsEndImageContext()
        
        // 5. 返回结果
        return result
    }

    // 懒加载放置二维码的imageview
    lazy var iconView:UIImageView = UIImageView()
    
    override func loadView() {
        // 设置view的属性，view默认的颜色是nil
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        view.addSubview(iconView)
        iconView.backgroundColor = UIColor.lightGrayColor()

        // 水平垂直居中对齐
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        // 宽高的约束
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[iconView(200)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["iconView":iconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[iconView(200)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["iconView":iconView]))
        
        
    }

}
